package org.erepublik.company.service;

import java.util.ArrayList;

import org.erepublik.company.ECountry;
import org.erepublik.company.product.Food;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONFoodService extends JSONProductService<Food> implements IFoodService {

    public JSONFoodService() {
        super("food");
    }
    
    @Override
    protected Food[] getProductFromJSONArray(JSONArray jsonArrayWithFood) throws JSONException {
        return JSONFoodService.getFoodFromJSONArray(jsonArrayWithFood).toArray(new Food[0]);
    }
    
    
    public static ArrayList<Food> getFoodFromJSONArray(JSONArray jsonArrayWithFood) throws JSONException {
        ArrayList<Food> food = new ArrayList<Food>();
        for (int i = 0; i < jsonArrayWithFood.length(); i++) {
            food.add( Food.getFoodFromJSON( (JSONObject)jsonArrayWithFood.get(i) ) );
        }
        return food;
    }
    

    @Override
    public Food[] getFood(ECountry country) {
        update();
        if (country==null)
            return allProducts;
        return productMap.get(country);
    }


    @Override
    protected Food[] getEmptyTable() {
        return new Food[0];
    }

    @Override
    public String getMinutesSinceFoodUpdate() {
        return getMinutesSinceUpdate();
    }

}
